package com.wjj.demo;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.wjj.demo.adapter.XXlistviewAdapter;
import com.wjj.demo.view.PullUpSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.inflate;

/**
 * Created by Wjj on 16/11/22.
 */
public class XXListViewActivity extends AppCompatActivity {
    private ListView lv;

    private LinearLayout invis;

    private LinearLayout include;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;


    private List<String> lists = new ArrayList<>();

    private PullUpSwipeRefreshLayout pullUpSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sticky);
//        invis = (LinearLayout) findViewById(R.id.invis);

        include = (LinearLayout) findViewById(R.id.include);
        btn1 = (Button) include.findViewById(R.id.btn1);
        btn2 = (Button) include.findViewById(R.id.btn2);


        for (int i = 0; i < 120; i++) {
            lists.add("data-----" + i);
        }

        lv = (ListView) findViewById(R.id.lv);
        View header = inflate(this, R.layout.stick_header, null);//头部内容
        lv.addHeaderView(header);//添加头部
//        View view = View.inflate(this, R.layout.stick_action, null);
//        lv.addHeaderView(view);//ListView条目中的悬浮部分 添加到头部
//        btn3 = (Button) view.findViewById(R.id.btn1);
//        btn4 = (Button) view.findViewById(R.id.btn2);


        lv.setAdapter(new XXlistviewAdapter(lists, this));
//        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(AbsListView absListView, int i) {
//            }
//
//            @Override
//            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//                Log.d("xxx",firstVisibleItem+">>>>>>>");
//                if (firstVisibleItem >= 1) {
//                    include.setVisibility(View.VISIBLE);
//                } else {
//
//                    include.setVisibility(View.GONE);
//                }
//            }
//        });


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (mPopWindow2 != null) {
                    mPopWindow2.dismiss();
                }
                if (mPopWindow != null) {
                    mPopWindow.dismiss();
                }
                Toast.makeText(XXListViewActivity.this, "int i" + i + "》》》》》" + "long l" + l, Toast.LENGTH_SHORT).show();
            }
        });


        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lv.setSelection(1);
                Toast.makeText(XXListViewActivity.this, "btn1", Toast.LENGTH_SHORT).show();
                if (mPopWindow2 != null) {
                    mPopWindow2.dismiss();
                }
                if (mPopWindow != null) {
                    mPopWindow.dismiss();
                }
                showPopupWindow();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lv.setSelection(1);
                if (mPopWindow != null) {
                    mPopWindow.dismiss();
                }
                if (mPopWindow2 != null) {
                    mPopWindow2.dismiss();
                }
                showPopupWindow2();
                Toast.makeText(XXListViewActivity.this, "btn2", Toast.LENGTH_SHORT).show();
            }
        });

//        btn3.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                lv.setSelection(1);
//                Toast.makeText(XXListViewActivity.this, "btn1", Toast.LENGTH_SHORT).show();
//                if (mPopWindow2 != null) {
//                    mPopWindow2.dismiss();
//                }
//                if (mPopWindow != null) {
//                    mPopWindow.dismiss();
//                }
//                showPopupWindow();
//            }
//        });
//
//        btn4.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                lv.setSelection(1);
//                if (mPopWindow != null) {
//                    mPopWindow.dismiss();
//                }
//                if (mPopWindow2 != null) {
//                    mPopWindow2.dismiss();
//                }
//                showPopupWindow2();
//                Toast.makeText(XXListViewActivity.this, "btn4", Toast.LENGTH_SHORT).show();
//            }
//        });


        pullUpSwipeRefreshLayout = (PullUpSwipeRefreshLayout) findViewById(R.id.pullUpSwipeRefreshLayout);
        //下拉刷新
        pullUpSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pullUpSwipeRefreshLayout.setRefreshing(false);
                    }
                }, 3000);
            }
        });

    }


    private PopupWindow mPopWindow;
    private PopupWindow mPopWindow2;

    private void showPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(XXListViewActivity.this).inflate(R.layout.popuplayout, null);
        mPopWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        mPopWindow.setContentView(contentView);
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopWindow.dismiss();
            }
        });
        //设置各个控件的点击响应
//        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
//        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
//        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);
//        tv1.setOnClickListener(this);
//        tv2.setOnClickListener(this);
//        tv3.setOnClickListener(this);
        //显示PopupWindow
//        View rootview = LayoutInflater.from(XXListViewActivity.this).inflate(R.layout.activity_sticky, null);
//        mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setFocusable(false);
//        mPopWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopWindow.showAsDropDown(btn1);

    }

    private void showPopupWindow2() {
        //设置contentView
        View contentView = LayoutInflater.from(XXListViewActivity.this).inflate(R.layout.popuplayout2, null);
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopWindow2.dismiss();
            }
        });
        mPopWindow2 = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        mPopWindow2.setContentView(contentView);
        //设置各个控件的点击响应
//        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
//        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
//        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);
//        tv1.setOnClickListener(this);
//        tv2.setOnClickListener(this);
//        tv3.setOnClickListener(this);
        //显示PopupWindow
//        View rootview = LayoutInflater.from(XXListViewActivity.this).inflate(R.layout.activity_sticky, null);
//        mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
        mPopWindow2.setOutsideTouchable(true);
        mPopWindow2.setFocusable(false);
        mPopWindow2.showAsDropDown(btn1);

    }


}
